﻿@{
    
    Layout = "~/Shared/Layouts/_Layout.cshtml";
    Page.Title = "Кошничка";
    
    // Create a blank cart in Session if not present
    if (Session["cart"] == null)
    {
        Session["cart"] = new Cart();
    }
    
    // Get current cart from Session
    Cart cart = (Cart)Session["cart"];
    
    if (IsPost)
    {
        if (Request["productID"] != null)
        {
            // Form posted from Product Details Page
            var productID = Request["productID"];
            var size = Request["size"];
            var price = Request["price"];
        
            cart.AddItem(productID.AsInt(), size, price.AsDecimal());
        }
        else if (Request["removeIndex"] != null)
        {
            // Form posted by Cart Remove Item button
            var removeIndex = Request["removeIndex"].AsInt();
            cart.RemoveItem(removeIndex);
        }
    }
    
}

<h1>Кошничка</h1>

@if(cart.Items.Count() == 0)
{
    <p>Нема производи во вашата кошничка.</p>
}
else
{
    <text>    
        <table id="cartTable">
            <tr>
                <th class="product">Производ</th>
                <th class="size">Големина</th>
                <th class="price">Цена</th>
                <th>&nbsp;</th>
            </tr>
            
            @{  
                // Declare and initialise index variable, i
                int i = 0;
            }
            
            @foreach (var item in cart.Items)
            {
                <tr>
                    <td class="product">@Products.GetProductNameFromId(item.ProductID)</td>
                    <td class="size">@item.Size</td>
                    <td class="price">£@item.Price</td>
                    <td class="remove">
                        <form action="Cart" method="post">
                            @Html.Hidden("removeIndex", i)
                            <input type="submit" value="Избриши" />
                        </form>
                    </td>
                </tr>
            
                // increment index
                i++;
            }
            
            <tr class="cartTotal">
                <td colspan="2">&nbsp;</td>
                <td>Вкупно: £@cart.TotalValue</td>
                <td>&nbsp;</td>
            </tr>
        </table>
        <div id="cartButtons">
            <a href="/Default" class="linkButton">Продолжи со купување</a>   
            <a href="/Checkout" class="linkButton">Продолжи со плаќање</a>    
        </div>
    </text>
}
